package cn.ustb.controller;

import cn.ustb.entity.ExamArrange;
import cn.ustb.entity.User;
import cn.ustb.service.ExamArrangeService;
import cn.ustb.serviceImpl.ExamArrangeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author yuyan
 */
@WebServlet("/examArrangeServlet.do")
public class ExamArrangeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        String yearTerm = request.getParameter("yearTerm");
        User user = (User) session.getAttribute("user");

        List<ExamArrange> examArranges;
        ExamArrangeService examArrangeService=new ExamArrangeServiceImpl();

        if(!"".equals(yearTerm)){
            examArranges = examArrangeService.getAllWithYearTerm(user.getUsername(), yearTerm);
        }else{
            examArranges = examArrangeService.getAllWithStudentId(user.getUsername());
        }
        System.out.println(examArranges);
        session.setAttribute("examArranges", examArranges);
        request.getRequestDispatcher("/examArrange.jsp").forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
